#include<bits/stdc++.h>
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
private:
    TreeNode* build(vector<int>& preorder, vector<int>& inorder, int i, int j, int k, int l){
        if(i > j)return nullptr;
        TreeNode *tree = new TreeNode(preorder[i]);
        for(int mov = k; mov <= l; mov++){
            if(inorder[mov] == preorder[i]){
                tree->left = build(preorder, inorder, i + 1, i + mov - k, k, mov - 1);
                tree->right = build(preorder, inorder, i + mov - k + 1, j, mov + 1, l);
            }
        }
        return tree;
    }
public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        return build(preorder, inorder, 0, preorder.size() - 1, 0, inorder.size() - 1);
    }
};

/**
 * @brief 
 * 2022.5.11
 * 注意范围
 */